package com.ruoyi.web.wx.controller.pay;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.system.domain.wx.pay.WxPayInfo;
import com.ruoyi.system.domain.wx.pay.WxPayType;
import com.ruoyi.system.domain.wx.WxUser;
import com.ruoyi.system.service.wx.pay.IWxPayInfoService;
import com.ruoyi.system.service.wx.pay.IWxPayTypeService;
import com.ruoyi.system.service.wx.IWxUserService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 支出明细Controller
 *
 * @author mic
 * @date 2023-08-14
 */
@RestController
@RequestMapping("/wxPayInfo")
public class WxPayInfoController extends BaseController {
    @Autowired
    private IWxPayInfoService wxPayInfoService;
    @Autowired
    private IWxPayTypeService wxPayTypeService;
    @Autowired
    private IWxUserService wxUserService;

    /**
     * 查询支出明细列表
     */
    @PreAuthorize("@ss.hasPermi('wxPayInfo:wxPayInfo:list')")
    @GetMapping("/list")
    public TableDataInfo list(WxPayInfo wxPayInfo) {
        startPage();
        List<WxPayInfo> list = wxPayInfoService.selectWxPayInfoList(wxPayInfo);
        List<WxPayType> payTypes = wxPayTypeService.selectWxPayTypeList(new WxPayType());
        List<WxPayType> sTypes = payTypes;
        for (int i = 0; i < list.size(); i++) {
            // 放入用户信息
            WxUser wxUser = wxUserService.selectWxUserByUserId(list.get(i).getCreateBy());
            if (wxUser != null) {
//                list.get(i).setUserId(wxUser.getUserId());
                list.get(i).setViewId(wxUser.getViewId());
            }
            // 合并支出类型
            for (int j = 0; j < payTypes.size(); j++) {
                // 查询到子类型
                if (list.get(i).getPrimaryType().equals(payTypes.get(j).getTypeId())) {
                    String pName = payTypes.get(j).getTypeName();
                    list.get(i).setPrimaryType(pName);
                    for (int k = 0; k < sTypes.size(); k++) {
                        if (payTypes.get(j).getParentId().equals(sTypes.get(k).getTypeId())) {
                            String sName = sTypes.get(k).getTypeName();
                            list.get(i).setPrimaryType(pName + " - " + sName);
                        }
                    }
                }
            }
        }

        return getDataTable(list);
    }

    /**
     * 导出支出明细列表
     */
    @PreAuthorize("@ss.hasPermi('wxPayInfo:wxPayInfo:export')")
    @Log(title = "支出明细", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, WxPayInfo wxPayInfo) {
        List<WxPayInfo> list = wxPayInfoService.selectWxPayInfoList(wxPayInfo);
        ExcelUtil<WxPayInfo> util = new ExcelUtil<WxPayInfo>(WxPayInfo.class);
        util.exportExcel(response, list, "支出明细数据");
    }

    /**
     * 获取支出明细详细信息
     */
    @PreAuthorize("@ss.hasPermi('wxPayInfo:wxPayInfo:query')")
    @GetMapping(value = "/{payId}")
    public AjaxResult getInfo(@PathVariable("payId") String payId) {
        return success(wxPayInfoService.selectWxPayInfoByPayId(payId));
    }

    /**
     * 新增支出明细
     */
    @PreAuthorize("@ss.hasPermi('wxPayInfo:wxPayInfo:add')")
    @Log(title = "支出明细", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WxPayInfo wxPayInfo) {
        return toAjax(wxPayInfoService.insertWxPayInfo(wxPayInfo));
    }

    /**
     * 修改支出明细
     */
    @PreAuthorize("@ss.hasPermi('wxPayInfo:wxPayInfo:edit')")
    @Log(title = "支出明细", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WxPayInfo wxPayInfo) {
        return toAjax(wxPayInfoService.updateWxPayInfo(wxPayInfo));
    }

    /**
     * 删除支出明细
     */
    @PreAuthorize("@ss.hasPermi('wxPayInfo:wxPayInfo:remove')")
    @Log(title = "支出明细", businessType = BusinessType.DELETE)
    @DeleteMapping("/{payIds}")
    public AjaxResult remove(@PathVariable String[] payIds) {
        return toAjax(wxPayInfoService.deleteWxPayInfoByPayIds(payIds));
    }
}
